<?php

namespace app\supplier\model\stall;

use app\common\model\product\Product as ProductModel;
use app\common\model\stall\Product as StallProductModel;

class Product extends ProductModel
{


    public function getProcuteList($stall_id=0,$cateid=0,$shop_supplier_id = 0)
    {
        $param = [
            'list_rows'=>20,
            'category_id'=>$cateid,
            'sortType'=>'name',
            'status' => -1,
            'shop_supplier_id' => $shop_supplier_id
        ];
        $model = new ProductModel();
        $list = $model->getListNoCity($param);
        foreach ($list as &$v) {
            $newrs = self::getStallProductStatus($stall_id,$v['product_id']);
            if($newrs){
                $v['sell_product_price'] = $newrs['product_price'];
                $v['is_show'] = $newrs['is_show'];
            }else{
                $v['sell_product_price'] = 0;
                $v['is_show'] = 1;
            }
        }
        return $list;
    }

    public function setStatus($postData)
    {
        $stall_id = $postData['stall_id'];
        $product_id = $postData['product_id'];
        $product_price = $postData['product_price'];
        $is_show = $postData['is_show'];

        //价格检测
        if($product_price<0){
            return false;
        }
        $saveData = [
            'product_id' => $product_id,
            'stall_id' => $stall_id,
            'product_price' => $product_price,
            'is_show' => $is_show,
            'create_time' => time(),
            'update_time' => time(),
            'app_id' => self::$app_id
        ];
        //检查是否存在
        $Strs = StallProductModel::checkDB($stall_id, $product_id);
        if (StallProductModel::updateShowStatus($Strs['id'] ?? 0, $saveData)) {
            return true;
        }
        return false;
    }

    private static function getStallProductStatus($stall_id, $productid = 0)
    {
        return StallProductModel::checkDB($stall_id, $productid);
    }
}